package me.edzh.spring.bootstrap.interceptor;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Edward
 * 2016/8/13.
 */
public class ContentTypeInterceptor extends HandlerInterceptorAdapter {
    private final Logger logger = Logger.getLogger(ContentTypeInterceptor.class);

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!"OPTIONS".equals(request.getMethod().toUpperCase())) {
            response.setContentType("application/json; charset=utf-8");
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        if (!"OPTIONS".equals(request.getMethod().toUpperCase())) {
            response.setContentType("application/json; charset=utf-8");
        }
    }
}